package com.lottery.ruok.lottrey.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.eventbusObject.ChargeSuccess;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.RechargeRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetPayTypeResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.RechargeResponse;
import com.lottery.ruok.lottrey.ui.component.BankInfoView;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.lottery.ruok.lottrey.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.NavigateBar;
import utils.CashierInputFilter;
import utils.Utils;
/*
 * 银行卡转账
 * */

public class ChargeBankActivity extends BaseActivity implements TextWatcher {
    public static final String EXTRA_MONEY = "EXTRA_MONEY";
    public static final String EXTRA_DATA = "EXTRA_DATA";

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.money)
    EditText money;
    @BindView(R.id.time)
    EditText time;
    @BindView(R.id.submit)
    TextView submit;
    @BindView(R.id.view_bankInfo)
    BankInfoView bankInfoView;
    @BindView(R.id.navigateBar)
    NavigateBar navigateBar;
    @BindView(R.id.tv_tip)
    TextView tipView;
    @BindView(R.id.ll_charge_name)
    LinearLayout ll_charge_name;
    @BindView(R.id.tv_charge_name_line)
    View tv_charge_name_line;
    @BindView(R.id.tv_cure)
    TextView tv_cure;
    private String bank_id;
    private String moneyValue;
    private GetPayTypeResponse.GetPayItemsResponse data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        setContentView(R.layout.transfer_bank_lottery_fragment);
        ButterKnife.bind(this);
        init();

    }

    private void initData() {
        moneyValue = getIntent().getStringExtra(EXTRA_MONEY);
        data = (GetPayTypeResponse.GetPayItemsResponse) getIntent().getSerializableExtra(EXTRA_DATA);
    }


    private void init() {
        navigateBar.setCenterTitle(data.getType_name());
        time.setText(Utils.toYMD_HMS(System.currentTimeMillis()));

        money.setText(moneyValue);
        InputFilter[] inputFilters = {new CashierInputFilter()};
        money.setFilters(inputFilters);
        name.addTextChangedListener(this);
        money.addTextChangedListener(this);
        time.addTextChangedListener(this);
        bankInfoView.setBankName(data.getBankname());
        bankInfoView.setBankNum(data.getCard_no());
        bankInfoView.setBankAddress(data.getBankson());
        bankInfoView.setUserName(data.getRealname());
        tipView.setText(data.getDesc());
        navigateBar.setOnIconClickListener(new NavigateBar.OnIconClickListener() {
            @Override
            public void OnIconClick(View view) {
                //SystemUtil.hiddenInputForce(ChargeArtificialActivity.this);
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                finish();
            }
        });
        if (data.getNo_name().equals("1")){
            ll_charge_name.setVisibility(View.GONE);
            tv_charge_name_line.setVisibility(View.GONE);

            submit.setEnabled(true);
            submit.setTextColor(Color.parseColor("#ffffff"));
        }else if (data.getNo_name().equals("0")){
            ll_charge_name.setVisibility(View.VISIBLE);
            tv_charge_name_line.setVisibility(View.VISIBLE);

        }

        if (StringUtils.isNotEmpty(data.getRemark())){
            tv_cure.setText("备注："+data.getRemark());
            tv_cure.setVisibility(View.VISIBLE);
        }else {
            tv_cure.setVisibility(View.GONE);
        }
    }

    private void changeSubmitButton() {


        //   if (name.getText().length() > 0 && money.getText().length() > 0 && time.getText().length() > 0) {
        if ( money.getText().length() > 0 && time.getText().length() > 0) {
            submit.setEnabled(true);
            submit.setTextColor(Color.parseColor("#ffffff"));
        } else {
            submit.setEnabled(false);
            submit.setTextColor(Color.parseColor("#9942F7"));
        }
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        changeSubmitButton();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @OnClick(R.id.submit)
    public void onViewClicked() {
        String moneyStr = money.getText().toString();
        if (Utils.parseFloat(moneyStr) < data.getMob_min()) {
            Utils.showToast("输入金额过小,最小金额为" + data.getMob_min());
            return;
        }
        if (Utils.parseFloat(moneyStr) > data.getMob_max()) {
            Utils.showToast("输入金额过大,最大金额为" + data.getMob_max());
            return;
        }
        //sendAnalyticMsg("lottery_" + ChargeActivity.ANALYTIC_VERSION + "_me_recharge_manual_bank_submit");
        String name1=name.getText().toString();
        if (data.getNo_name().equals("1")){
            name1=data.getName_alias();
        }

        RetrofitBase.getRetrofit().create(RechargeRequest.class).getResult(UserInfo.getToken(), money.getText().toString(), data.getId(), "0", null, "暂无", "暂无", name1, time.getText().toString())
                .compose(RxHelper.<RechargeResponse>handleResult())
                .subscribe(new RxSubscribe<RechargeResponse>(this) {
                    @Override
                    protected void _onNext(RechargeResponse rechargeResponse) {
                        EventBus.getDefault().post(new ChargeSuccess());

//                        Utils.showToast("如有充值请点击刷新余额");
                        Utils.showToast("汇款信息提交成功");
                        finish();
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    public static void startActivity(Activity activity, GetPayTypeResponse.GetPayItemsResponse data, String money) {
        Intent intent = new Intent(activity, ChargeBankActivity.class);
        intent.putExtra(EXTRA_MONEY, money);
        intent.putExtra(EXTRA_DATA, data);
        activity.startActivity(intent);
    }
}
